package com.team3.project.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.team3.project.dao.LeaveDAO;
import com.team3.project.exception.LeaveNotFoundException;
import com.team3.project.model.LeaveRecord;

public class LeaveDAOImpl implements LeaveDAO {

	private static final Log log = LogFactory.getLog(UserDAOImpl.class);
	private static Session session=null;
	private static Transaction tx = null;
	private static Query query =null;
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private void shutdown() {
		// TODO Auto-generated method stub
		session.close();
	}

	@Override
	public void save(LeaveRecord leaveRecord) {
		try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		
    		session.saveOrUpdate(leaveRecord);
    	
    		tx.commit();
 
    	}catch(RuntimeException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}finally{
    		if(session!=null){
    			shutdown();
    		}
    	} 
		
	}

	@Override
	public void update(LeaveRecord leaveRecord) throws LeaveNotFoundException {
		try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		
    		LeaveRecord l = (LeaveRecord) session.load(LeaveRecord.class, leaveRecord.getRefNo());
    		if(l.equals(null))
    		{
    			throw new LeaveNotFoundException("NOT FOUND RECORD TO DELETE");
    		}
    		session.update(leaveRecord);
    		
    		tx.commit();
 
    	}catch(RuntimeException|LeaveNotFoundException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}finally{
    		if(session!=null){
    			shutdown();
    		}
    	}
		
	}

	@Override
	public void delete(LeaveRecord leaveRecord) throws LeaveNotFoundException {
		try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		
    		LeaveRecord l = (LeaveRecord) session.load(LeaveRecord.class, leaveRecord.getRefNo());
    		if(l.equals(null))
    		{
    			throw new LeaveNotFoundException("NOT FOUND RECORD TO DELETE");
    		}
    		session.delete(l);
    	
    		tx.commit();
 
    	}catch(RuntimeException|LeaveNotFoundException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}
    	finally{
    		if(session!=null){
    			shutdown();
    		}
    	}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public LeaveRecord findByRefNo(int refNO) throws LeaveNotFoundException {
		List<LeaveRecord> list =null;
	   	try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		
    		query = session.createQuery("from LeaveRecord where refNO = :refNO");
    		query.setParameter("id", refNO);
    		list = query.list();
    		if(list==null||list.isEmpty())
    		{
    			throw new LeaveNotFoundException("NOT FOUND RECORD !!!");		
    		}
    	
    		tx.commit();
 
    	}catch(RuntimeException|LeaveNotFoundException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}finally{
    		if(session!=null){
    			session.close();
    		}
    	}
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LeaveRecord> findAll() {
		List<LeaveRecord> list;
	   	try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
  
    		query = session.createQuery("from LeaveRecord");
    		list =  query.list();
    		
    		tx.commit();
 
    	}catch(RuntimeException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}finally{
    		if(session!=null){
    			session.close();
    		}
    	}
		return list;
	}

	@Override
	public void deleteByRefNo(int RefNO) throws LeaveNotFoundException {
		try{
    		session = getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);
    		
    		LeaveRecord l = (LeaveRecord) session.load(LeaveRecord.class, RefNO);
    		if(l.equals(null))
    		{
    			throw new LeaveNotFoundException("NOT FOUND RECORD TO DELETE");
    		}
    		session.delete(l);
    	
    		tx.commit();
 
    	}catch(RuntimeException|LeaveNotFoundException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}
    	finally{
    		if(session!=null){
    			shutdown();
    		}
    	}
		
	}
 }
